package com.vg.view;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;

import vg.com.chatonline.activity.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
import ce51.dunghn.chatslide.LeftAndRightActivity;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.download.URLConnectionImageDownloader;
import com.nostra13.universalimageloader.utils.FileUtils;

public class ViewSingleImage extends Activity {
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	ImageView imgView;
	String imageUrl;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_view_image);
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.no)
				.showStubImage(R.drawable.no).cacheInMemory().cacheOnDisc()
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		imgView = (ImageView) findViewById(R.id.view_imgview);
		Bundle bdl = getIntent().getExtras();
		imageUrl = bdl.getString("IMAGE");
		imageLoader.displayImage(imageUrl, imgView, options);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, 0, Menu.NONE, "Xem thông tin");
		menu.add(Menu.NONE, 1, Menu.NONE, "Nhắn tin");
		menu.add(Menu.NONE, 2, Menu.NONE, "Tải về");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			finish();
			return true;
		case 1:
			Intent i = new Intent(getApplicationContext(),
					LeftAndRightActivity.class);
			startActivity(i);
			return true;
		case 2:
			// Tao folder moi neu chua co
			String path = Environment.getExternalStorageDirectory().toString();
			File file = new File(path + "/chatvg/");
			if (!file.exists()) {
				new File(path + "/chatvg/").mkdir();
			}
			
			File imgName = new File(path + "/chatvg/" + getImageName(imageUrl));
			InputStream sourceStream;
			File cachedImage = ImageLoader.getInstance().getDiscCache()
					.get(imageUrl);
			try {
				if (cachedImage.exists()) {
					sourceStream = new FileInputStream(cachedImage);
				} else {
					ImageDownloader downloader = new URLConnectionImageDownloader();
					sourceStream = downloader.getStream(new URI(imageUrl));
				}

				OutputStream targetStream = new FileOutputStream(imgName);
				FileUtils.copyStream(sourceStream, targetStream);
				targetStream.close();
				sourceStream.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (URISyntaxException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} 
			Toast.makeText(getBaseContext(), "Tai xong", Toast.LENGTH_LONG).show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	// Tra ve ten cua anh tu url
	public String getImageName(String url) {
		String imgName = "";
		String token[] = new String[url.split("/").length];
		token = url.split("/");
		imgName = token[token.length - 1];
		return imgName;
	}
}
